package top.wshape1.easyscan.custom;

import android.app.Dialog;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import top.wshape1.easyscan.R;
import top.wshape1.easyscan.activity.EasyScanActivity;
import top.wshape1.easyscan.adapter.CustomRuleSpinnerAdapter;
import top.wshape1.easyscan.entity.CustomRule;

/**
 * @author Wshape1
 * @version 1.0
 * @description
 * @since 2023-06-17
 */
public class CustomRuleAddDialog extends Dialog {
    private View customRuleAddLayout;

    private CustomRule customRule = new CustomRule();

    private EasyScanActivity mainActivity;

    private EditText etDescription;

    private EditText etMatchString;

    private Spinner spType;

    private Spinner spOrder;

    private Spinner spApp;


    public CustomRuleAddDialog(@NonNull Context context) {
        super(context, R.style.BackgroundTransparentStyle);
        mainActivity = (EasyScanActivity) context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        customRuleAddLayout = View.inflate(mainActivity, R.layout.dialog_custom_rule_add, null);

        setContentView(customRuleAddLayout);

        Button btnDelete = customRuleAddLayout.findViewById(R.id.btn_custom_rule_delete);

        // init View
        etDescription = customRuleAddLayout.findViewById(R.id.et_custom_rule_description);
        etMatchString = customRuleAddLayout.findViewById(R.id.et_custom_rule_string);
        spType = customRuleAddLayout.findViewById(R.id.sp_custom_rule_type);
        spOrder = customRuleAddLayout.findViewById(R.id.sp_custom_rule_order);
        spApp = customRuleAddLayout.findViewById(R.id.sp_custom_rule_app);

        CustomRuleSpinnerAdapter customRuleSpinnerAdapter = new CustomRuleSpinnerAdapter(mainActivity, mainActivity.applicationInfoList);
        spApp.setAdapter(customRuleSpinnerAdapter);

        // event

        spType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // pos 对应 type的值
                customRule.setType(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        spOrder.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // 显示的pos跟order对应
                customRule.setOrder(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        spApp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ApplicationInfo info = customRuleSpinnerAdapter.getItem(position);
                customRule.setClassName(info.loadLabel(mainActivity.getPackageManager()).toString());
                customRule.setPackageName(info.packageName);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        customRuleAddLayout.findViewById(R.id.iv_custom_rule_add_back).setOnClickListener(v -> {
            new CustomRuleListDialog(mainActivity).show();
            dismiss();
        });
        customRuleAddLayout.findViewById(R.id.btn_custom_rule_save).setOnClickListener(v -> {

            if ("".equals(etDescription.getText().toString().trim()) || "".equals(etMatchString.getText().toString().trim())) {
                Toast.makeText(mainActivity, "请检查是否全部填写", Toast.LENGTH_LONG).show();
                return;
            }

            customRule.setDescription(etDescription.getText().toString());
            customRule.setMatchString(etMatchString.getText().toString());


            if (customRule.getId() == null) {
                mainActivity.settingData.addCustomRule(customRule);
            } else {
                mainActivity.settingData.updateCustomRule(customRule);
            }

            new CustomRuleListDialog(mainActivity).show();
            dismiss();
        });

        btnDelete.setOnClickListener(v -> {
            if (customRule.getId() != null) {
                mainActivity.settingData.removeCustomRule(customRule.getId());
                new CustomRuleListDialog(mainActivity).show();
                dismiss();
            }
        });

        if (customRule.getId() != null) {
            btnDelete.setVisibility(View.VISIBLE);
            ((TextView) customRuleAddLayout.findViewById(R.id.tv_custom_rule_title)).setText("修改自定义规则");

            if (customRule.getDescription() != null)
                etDescription.setText(customRule.getDescription());
            if (customRule.getMatchString() != null)
                etMatchString.setText(customRule.getMatchString());
            if (customRule.getType() != null)
                spType.setSelection(customRule.getType());
            if (customRule.getOrder() != null)
                spOrder.setSelection(customRule.getOrder());
            if (customRule.getClassName() != null && customRule.getPackageName() != null) {
                ApplicationInfo applicationInfo = mainActivity.goalAppInfoMap.get(customRule.getPackageName());
                if (applicationInfo != null)
                    spApp.setSelection(mainActivity.applicationInfoList.indexOf(applicationInfo));
                else
                    Toast.makeText(mainActivity, "找不到应用：" + customRule.getClassName(), Toast.LENGTH_LONG).show();
            }

        }

    }

    public void setCustomRule(@NonNull CustomRule customRule) {
        this.customRule = customRule;
    }

}
